package com.example.webviewmodule

import android.graphics.Canvas
import android.graphics.SurfaceTexture
import android.opengl.GLES11Ext
import android.opengl.GLES20
import android.opengl.GLSurfaceView
import android.opengl.GLUtils
import android.util.Log
import android.view.Surface
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

open class BaseWebGLRenderer(var mTextureWidth:Int, var mTextureHeight:Int):GLSurfaceView.Renderer{

    companion object {
        private val TAG=BaseWebGLRenderer::class.simpleName
    }

    var mGlSurfaceTexture: Int = 0

    private  var mSurfaceTexture: SurfaceTexture?=null

    private var mSurface: Surface?=null

    private var mSurfaceCanvas: Canvas? = null

    override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {
        releaseSurface();
        mGlSurfaceTexture=createTexture()
        if(mGlSurfaceTexture>0){
            mSurfaceTexture=SurfaceTexture(mGlSurfaceTexture)
            mSurfaceTexture?.setDefaultBufferSize(mTextureWidth,mTextureHeight)
            mSurface=Surface(mSurfaceTexture)

        }
    }

    private fun releaseSurface() {
        mSurface?.release()
        mSurfaceTexture?.release()
        mSurface=null
        mSurfaceTexture=null
    }

    private fun createTexture(): Int {
        val textures=IntArray(1)

        GLES20.glActiveTexture(GLES20.GL_TEXTURE0)
        GLES20.glGenTextures(1,textures,0)
        checkGlError("Texture generate")

        GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,textures[0])
        checkGlError("Texture bind")

        GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_LINEAR.toFloat())
        GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_LINEAR.toFloat())
        GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,GL10.GL_TEXTURE_WRAP_S,
            GL10.GL_CLAMP_TO_EDGE.toFloat()
        )
        GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,GL10.GL_TEXTURE_WRAP_T,GL10.GL_CLAMP_TO_EDGE.toFloat())
        return textures[0]

    }

    private fun checkGlError(s: String) {

        while (GLES20.glGetError()!=GLES20.GL_NO_ERROR){
            var error=GLES20.glGetError()
            Log.e(TAG,"${s} : glError "+GLUtils.getEGLErrorString(error))

        }
    }

    override fun onSurfaceCreated(gl: GL10, config: EGLConfig) {
        val extensions=GLES20.glGetString(GLES20.GL_EXTENSIONS)
        Log.d(TAG,extensions)
    }


    override fun onDrawFrame(gl: GL10?) {
        synchronized(this){
            mSurfaceTexture?.updateTexImage()
        }
    }

    fun onDrawViewBegin(): Canvas? {
        mSurfaceCanvas=null
        mSurface?.let {
            mSurfaceCanvas=it.lockCanvas(null)
        }
        return mSurfaceCanvas
    }

    fun onDrawViewEnd(){
        mSurfaceCanvas?.let {
            mSurface?.unlockCanvasAndPost(mSurfaceCanvas)
        }
        mSurfaceCanvas=null
    }





}